<?
    final class SettingsController extends PrincipioLab_Controller_Action
    {
        const ACTION_EDIT = 1;


        public function init()
        {
            parent::init();
        }


        private function setActionViewData($action)
        {
            switch ($action)
            {
                case self::ACTION_EDIT:
                    Principio_Common::setButtons(
                        "Apply",
                        "ajaxSettings({id: ".$this->view->settings_id.", action: 'edit', use_inner: true});",
                        "ajaxSettings({id: ".$this->view->settings_id.", action: 'edit', use_inner: true, no_apply: true});"
                    );
                    break;
            };
        }


        public function indexAction()
        {
            $this->view->title = "Settings";
        }


        public function editAction()
        {
            if (($this->is_post) && ($this->checkPostItemInt("id", "Settings ID")))
            {
                $this->view->settings_id = $this->post_data['id'];
                $this->setActionViewData(self::ACTION_EDIT);

                switch ($this->view->settings_id)
                {
                    case 1:
                        $this->view->title = "Default profile settings";
                        $this->view->model_setting_template_global = Principio_Model_SettingMapper::fetch(Principio_Model_SettingMapper::SETTING_GLOBAL_TEMPLATE_GLOBAL);
                        $this->view->model_setting_template_ext = Principio_Model_SettingMapper::fetch(Principio_Model_SettingMapper::SETTING_GLOBAL_TEMPLATE_EXT);

                        if ($this->is_apply)
                        {
                            $this->checkPostItemSetting($this->view->model_setting_template_global);
                            $this->checkPostItemSetting($this->view->model_setting_template_ext);

                            if (!Principio_Common::$is_error)
                            {
                                Principio_Model_SettingMapper::save($this->view->model_setting_template_global);
                                Principio_Model_SettingMapper::save($this->view->model_setting_template_ext);

                                if (!Principio_Common::$is_error)
                                    Principio_Common::reportMessage("Templates were changed.");
                            };
                        };
                    break;


                    case 2: // DNS
                        $this->view->title = "DNS Options";
                        $this->view->model_option_dns_primary = Principio_Model_OptionMapper::fetch(Principio_Model_OptionMapper::OPTION_DNS_PRIMARY);
                        $this->view->model_option_dns_secondary = Principio_Model_OptionMapper::fetch(Principio_Model_OptionMapper::OPTION_DNS_SECONDARY);
                        $this->view->model_option_dns_server_order = Principio_Model_OptionMapper::fetch(Principio_Model_OptionMapper::OPTION_DNS_SERVER_ORDER);
                        $this->view->model_option_dns_query_mode = Principio_Model_OptionMapper::fetch(Principio_Model_OptionMapper::OPTION_DNS_QUERY_MODE);

                        if ($this->is_apply)
                        {
                            $this->checkPostItemSettingIP($this->view->model_option_dns_primary, "Primary DNS");
                            $this->checkPostItemSettingIP($this->view->model_option_dns_secondary, "Secondary DNS");

                            if ($this->checkPostItemSettingString($this->view->model_option_dns_server_order, "Server order"))
                            {
                                if (
                                    ($this->view->model_option_dns_server_order->getValue() != "Manual") &&
                                    ($this->view->model_option_dns_server_order->getValue() != "Manual,DHCP") &&
                                    ($this->view->model_option_dns_server_order->getValue() != "DHCP,Manual")
                                    )
                                    Principio_Common::reportError("Wrong [Server order] value [".$this->view->model_option_dns_server_order->getValue()."].");

                            };

                            if ($this->checkPostItemSettingString($this->view->model_option_dns_query_mode, "Query mode"))
                            {
                                if (
                                    ($this->view->model_option_dns_query_mode->getValue() != "Parallel") &&
                                    ($this->view->model_option_dns_query_mode->getValue() != "Sequential")
                                    )
                                    Principio_Common::reportError("Wrong [Query mode] value [".$this->view->model_option_dns_query_mode->getValue()."].");
                            };

                            if (!Principio_Common::$is_error)
                            {
                                Principio_Model_OptionMapper::save($this->view->model_option_dns_primary);
                                Principio_Model_OptionMapper::save($this->view->model_option_dns_secondary);
                                Principio_Model_OptionMapper::save($this->view->model_option_dns_server_order);
                                Principio_Model_OptionMapper::save($this->view->model_option_dns_query_mode);

                                if (!Principio_Common::$is_error)
                                    Principio_Common::reportMessage("DNS options were changed.");
                            };

                        };
                        break;


                    case 3:
                        $this->view->title = "View and Edit Audio Codecs Settings (Not Implemented)";
                        $this->view->codecs = Principio_Model_SettingMapper::getAudioCodecs();
                    break;


                    case 6: // TFTP
                        $this->view->title = "TFTP settings";
                        $this->view->model_setting_tftp_path_to_initial_configs = Principio_Model_SettingMapper::fetch(Principio_Model_SettingMapper::SETTING_TFTP_PATH_TO_INITIAL_CONFIGS);
                        $this->view->model_setting_tftp_default_config = Principio_Model_SettingMapper::fetch(Principio_Model_SettingMapper::SETTING_TFTP_DEFAULT_CONFIG);

                        $this->view->model_setting_profile_rule_HTTP = Principio_Model_SettingMapper::getProfileRuleHTTP();
                        $model_setting_tftp_path_to_initial_configs_old = clone $this->view->model_setting_tftp_path_to_initial_configs;

                        if ($this->is_apply)
                        {
                            $this->checkPostItemSettingString($this->view->model_setting_tftp_path_to_initial_configs, "Path to initial configs");
                            $this->checkPostItemSetting($this->view->model_setting_tftp_default_config);

                            if (!Principio_Common::$is_error)
                            {
                                Principio_Model_SettingMapper::save($this->view->model_setting_tftp_path_to_initial_configs);
                                Principio_Model_SettingMapper::save($this->view->model_setting_tftp_default_config);

                                if (!Principio_Common::$is_error)
                                    Principio_Common::reportMessage("TFTP settings were changed.");

                                if (!Principio_Common::$is_error)
                                {

                                    $types = Principio_Model_TypeMapper::fetchAll();
                                    foreach ($types as $model_type)
                                    {
                                        if (Principio_Common::$is_error)
                                            break;

                                        $tftp_filename_new = $this->view->model_setting_tftp_path_to_initial_configs->getValue()."/".$model_type->getTFTPFilename();

                                        // TFTP configs relocation
                                        if ($model_setting_tftp_path_to_initial_configs_old->getValue() != $this->view->model_setting_tftp_path_to_initial_configs->getValue())
                                        {
                                            $tftp_filename_old = $model_setting_tftp_path_to_initial_configs_old->getValue()."/".$model_type->getTFTPFilename();

                                            if (Principio_Common::checkValidity(
                                                @rename($tftp_filename_old, $tftp_filename_new),
                                                "Can't move TFTP config for type [".$model_type->getCaption()."] from [".$tftp_filename_old."] to [".$tftp_filename_new."]."
                                            ))
                                                Principio_Common::reportMessage("TFTP config for type [".$model_type->getCaption()."] was moved from [".$tftp_filename_old."] to [".$tftp_filename_new."].");
                                        };


                                        if (!Principio_Common::$is_error)
                                        {
                                            if ($model_type->getUseDefaultTFTPConfig() == 1)
                                            {
                                                if (@file_put_contents($tftp_filename_new, $model_type->getFullTFTPConfig(), LOCK_EX) === false)
                                                    Principio_Common::reportError(
                                                        "Can't update TFTP config for type [".$model_type->getCaption()."] in a file [".$tftp_filename_new."].",
                                                        false);
                                                else
                                                    Principio_Common::reportMessage("TFTP config for type [".$model_type->getCaption()."] was updated.");
                                            };
                                        };
                                    };
                                };
                            };
                        };
                        break;


                    case 7: // DHCP
                        $this->view->title = "DHCP settings";

                        $this->view->model_setting_dhcp_restart_command = Principio_Model_SettingMapper::fetch(Principio_Model_SettingMapper::SETTING_DHCP_RESTART_COMMAND);
                        $this->view->model_setting_dhcp_config_file = Principio_Model_SettingMapper::fetch(Principio_Model_SettingMapper::SETTING_DHCP_CONFIG_FILE);

                        if ($this->is_apply)
                        {
                            $this->checkPostItemSettingString($this->view->model_setting_dhcp_restart_command, "Restart command");
                            $this->checkPostItemSettingString($this->view->model_setting_dhcp_config_file, "Config file");

                            if (!Principio_Common::$is_error)
                            {
                                Principio_Model_SettingMapper::save($this->view->model_setting_dhcp_restart_command);
                                Principio_Model_SettingMapper::save($this->view->model_setting_dhcp_config_file);

                                if (!Principio_Common::$is_error)
                                    Principio_Common::reportMessage("DHCP settings were changed.");
                            };
                        };
                        break;


                    case 8: // Common
                        $this->view->title = "Common settings";
                        $this->view->model_setting_hostname = Principio_Model_SettingMapper::fetch(Principio_Model_SettingMapper::SETTING_HOSTNAME);
                        $this->view->model_option_resync_periodic = Principio_Model_OptionMapper::fetch(Principio_Model_OptionMapper::OPTION_RESYNC_PERIODIC);

                        if ($this->is_apply)
                        {
                            $this->checkPostItemSettingString($this->view->model_setting_hostname, "Hostname");
                            $this->checkPostItemSettingInt($this->view->model_option_resync_periodic, "Resync periodic");

                            if (!Principio_Common::$is_error)
                            {
                                Principio_Model_SettingMapper::save($this->view->model_setting_hostname);
                                Principio_Model_OptionMapper::save($this->view->model_option_resync_periodic);

                                if (!Principio_Common::$is_error)
                                    Principio_Common::reportMessage("Settings were saved.");
                            };
                        };
                        break;


                    case 9: // Web Access
                        $this->view->title = "Web access options";

                        $this->view->model_option_web_server_enable = Principio_Model_OptionMapper::fetch(Principio_Model_OptionMapper::OPTION_WEB_SERVER_ENABLE);
                        $this->view->model_option_web_server_port = Principio_Model_OptionMapper::fetch(Principio_Model_OptionMapper::OPTION_WEB_SERVER_PORT);
                        $this->view->model_option_web_admin_access_enable = Principio_Model_OptionMapper::fetch(Principio_Model_OptionMapper::OPTION_WEB_ADMIN_ACCESS_ENABLE);
                        $this->view->model_option_web_admin_password = Principio_Model_OptionMapper::fetch(Principio_Model_OptionMapper::OPTION_WEB_ADMIN_PASSWORD);

                        if ($this->is_apply)
                        {
                            $this->checkPostItemSettingCheckbox($this->view->model_option_web_server_enable, "Enable web server");

                            if (!Principio_Common::$is_error)
                            {
                                if ($this->view->model_option_web_server_enable->getValue() == "Yes")
                                {
                                    $this->checkPostItemSettingInt($this->view->model_option_web_server_port, "Port");
                                    $this->checkPostItemSettingCheckbox($this->view->model_option_web_admin_access_enable, "Enable admin access");

                                    if (!Principio_Common::$is_error)
                                    {
                                        if ($this->view->model_option_web_admin_access_enable->getValue() == "Yes")
                                            $this->checkPostItemSettingString($this->view->model_option_web_admin_password, "Admin password");
                                        else
                                            $this->view->model_option_web_admin_password->setValue("");
                                    };
                                }
                                else
                                {
                                    $this->view->model_option_web_server_port->setValue("");
                                    $this->view->model_option_web_admin_access_enable->setValue("No");
                                    $this->view->model_option_web_admin_password->setValue("");
                                };
                            };


                            if (!Principio_Common::$is_error)
                            {
                                Principio_Model_OptionMapper::save($this->view->model_option_web_server_enable);
                                Principio_Model_OptionMapper::save($this->view->model_option_web_server_port);
                                Principio_Model_OptionMapper::save($this->view->model_option_web_admin_access_enable);
                                Principio_Model_OptionMapper::save($this->view->model_option_web_admin_password);

                                if (!Principio_Common::$is_error)
                                    Principio_Common::reportMessage("Web access options were changed.");
                            };
                        };
                        break;


                    default:
                        Principio_Common::reportError("Wrong settings ID.");
                };
            };
        }
    }
?>